﻿@using paycircuit.com.google.iap;
@*
 * Copyright (C) 2011 Paycircuit.com, Thinking Spot
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 *@
@functions {

    //*** I M P O R T A N T ***
    //use your credentials
    //This is a simple sample, you can define these in web.config (or _AppStart.cshtml options) as needed
    //When copying/pasting make sure you don't include spaces
    string MY_SELLER_ID = "YOUR SELLER ID";
    string MY_SELLER_SECRET = "YOUR SELLER SECRET";


    /***** I M P O R T A N T ***
     * This sample DOES NOT INCLUDE required code for you to confirm/verify payment
     * You must add code to check/verify successful paymenst/handling of your callback handler which is processed asynchronously (separate from this web browser/client process).
     */

    string theJWT()
    {
        try
        {
            JWTHeaderObject HeaderObj = new JWTHeaderObject(JWTHeaderObject.JWTHash.HS256, "1", "JWT");
            InAppItemObject ClaimObj = new InAppItemObject("Some Widget App", "ASP.NET web pages razor test", "4.99", "USD", "some seller unique data", MY_SELLER_ID, 2);
            return JWTHelpers.buildJWT(HeaderObj, ClaimObj, MY_SELLER_SECRET);
        }
        catch
        {
            return string.Empty;
        }
    }

    //Example using _AppStart.cshtml to store your credentials
    //See _AppStart.cshtml
    string theJWTv2()
    {
        try
        {
            JWTHeaderObject HeaderObj = new JWTHeaderObject(JWTHeaderObject.JWTHash.HS256, "1", "JWT");
            InAppItemObject ClaimObj = new InAppItemObject("Some Widget App 2", "ASP.NET web pages razor test 2 using Appstate", "4.99", "USD", "some seller unique data 2", @AppState["MY_SELLER_ID"].ToString(), 2);
            return JWTHelpers.buildJWT(HeaderObj, ClaimObj, @AppState["MY_SELLER_SECRET"].ToString());
        }
        catch
        {
            return string.Empty;
        }
    }
}
@{
    Page.Title = "In App Payments Sample";
    Layout = "~/shared/_layout.cshtml";
}
@section head {
    <meta name="description" content="A simple in-app payments demo page" />
    <meta name="author" content="edsf" />
    <script type="text/javascript" src="http://www.google.com/jsapi">        //you should get your own Google key for this as well</script>
    <script type="text/javascript">
        google.load('payments', '1.0', {
            'packages': ['sandbox_config']

        });

        function purchase(callback) {
            goog.payments.inapp.buy({
                "parameters": {},
                "jwt": "@Html.Raw(theJWT())",
                "success": function (result) {
                    if (typeof callback === "function") {
                        callback(true, result);
                    }
                },
                "failure": function (result) {
                    if (typeof callback === "function") {
                        callback(false, result);
                    }
                }
            }
        )
        };

        function purchase2(callback) {
            goog.payments.inapp.buy({
                "parameters": {},
                "jwt": "@Html.Raw(theJWTv2())",
                "success": function (result) {
                    if (typeof callback === "function") {
                        callback(true, result);
                    }
                },
                "failure": function (result) {
                    if (typeof callback === "function") {
                        callback(false, result);
                    }
                }
            }
        )
        };

        /*** S A M P L E   O N L Y ****
        *******************************
        !You should verify server side!
        *******************************                
        */
        var sampleParseResult = function (isgood, data) {
            var _console = (typeof window.console === "undefined");
            if (isgood) {
                var _str = "Verify Order No. " + data.response.orderId;
                _str += "\nDetails:\n";
                _str += data.request.name + " " + data.request.description + "\n";
                _str += data.request.price + "\n";
                alert(_str);
                if (!_console) {
                    console.log(data);
                }
            } else {
                alert("failed");
                if (!_console) {
                    console.log(data);
                }
            }
        };

    </script>
}
<div class="container_12" style="margin-top: 20px;">
    <div>
        <h1>
            In-App Payments Demo</h1>
        <h5>
            Some Widget App</h5>
        <ul>
            <li>My Widget makes your Android Tablet fly</li>
            <li>Price: $4.99</li>
        </ul>
        <div class="grid_6">
            <input id="foo" type="button" onclick="purchase(sampleParseResult);" value="Buy this widget"
                style="padding: 5px;" /></div>
        <div class="grid_6">
            <p>
                This test uses _AppStart.cshtml</p>
            <input id="bar" type="button" onclick="purchase2(sampleParseResult);" value="Buy this widget"
                style="padding: 5px;" />
        </div>
    </div>
</div>
